package gui;

import model.Model;
import model.GUIListener;

import javax.swing.*;
import java.util.Properties;
import java.awt.*;
import java.awt.event.*;

import gui.image.ImageComponent;
import util.Config;

public class MainFrame extends JFrame {

    private final ImagePanel imagePanel;

    private final JButton edit;

    private final Model model;

    private final JPanel sketchPanel;

    public MainFrame(String title, final Model model, Properties properties) {
        super(title);
        this.model = model;
        getContentPane().setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel buttonPanel = new JPanel();
        final JButton create = new JButton(properties.getProperty("button.create"));
        edit = new JButton(properties.getProperty("button.edit"));
        edit.setEnabled(false);
        buttonPanel.add(create);
        buttonPanel.add(edit);

        imagePanel = new ImagePanel(model);

        sketchPanel = new JPanel(new FlowLayout());

        model.setGUIListener(new GUIListener() {
            public void addSketch(int imageID) {
                MainFrame.this.addSketch(imageID);
            }

            public void imageChange(int imageID) {
                imagePanel.update(imageID);
                ImageComponent ic = null;
                for (Component c : sketchPanel.getComponents()) {
                    ImageComponent image = (ImageComponent) c;
                    if (imageID == image.getID()) {
                        ic = image;
                    }
                }
                sketchPanel.remove(ic);
                addSketch(imageID);
            }

            @Override
            public void removeImage(int imageID) {
                ImageComponent ic = null;
                for (Component c : sketchPanel.getComponents()) {
                    ImageComponent image = (ImageComponent) c;
                    if (imageID == image.getID()) {
                        ic = image;
                    }
                }
                sketchPanel.remove(ic);
                sketchPanel.revalidate();
                sketchPanel.repaint();
                if (imagePanel.getID() == imageID) {
                    imagePanel.update(-1);
                    edit.setEnabled(false);
                    pack();
                }
            }
        });

        create.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int id = model.createImage();
                imagePanel.update(id);
                addSketch(id);
                edit.setEnabled(false);
                imagePanel.setEditable(true);
            }
        });

        edit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int id = imagePanel.getID();
                model.addEditImage(id);
                edit.setEnabled(false);
                imagePanel.setEditable(true);
            }
        });

        getContentPane().add(buttonPanel, BorderLayout.NORTH);
        JScrollPane jsp = new JScrollPane(sketchPanel,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        Dimension d = new Dimension(Integer.parseInt(properties.getProperty("sketch.panel.width")),
                Integer.parseInt(properties.getProperty("sketch.panel.height")));
        jsp.setMinimumSize(d);
        jsp.setPreferredSize(d);

        getContentPane().add(jsp, BorderLayout.SOUTH);
        getContentPane().add(imagePanel, BorderLayout.CENTER);
        pack();
    }

    private final MouseAdapter ml = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            ImageComponent sketch = (ImageComponent) e.getSource();
            imagePanel.update(sketch.getID());
            int ind = -1;
            int i = 0;
            for (Component c : sketchPanel.getComponents()) {
                if (c == sketch) {
                    ind = i;
                }
                i++;
            }
            sketchPanel.remove(sketch);
            addSketch(sketch.getID(), ind);
            edit.setEnabled(!model.isEditable(sketch.getID()));
            imagePanel.setEditable(model.isEditable(sketch.getID()));
            pack();
        }
    };


    private void addSketch(int imageID) {
        addSketch(imageID, sketchPanel.getComponentCount());
    }

    private void addSketch(int imageID, int ind) {
        Config config = Config.getInstance();
        ImageComponent sketch = new ImageComponent(model.getSketch(imageID),
                config.getSketchWidth(), config.getSketchHeight(), imageID);
        sketchPanel.add(sketch, ind);
        sketch.addMouseListener(ml);
        sketchPanel.revalidate();
    }


}